import {
	msgHomeData,
	msgChatData
} from '@/object/msg.js'

export default {
	//开启命名空间
	namespaced: true,

	//数据节点
	state: () => ({
		//聊天数据
		chatData: uni.getStorageSync('chatData_' + uni.getStorageSync('userId')) || [],
		//置顶数据
		topMsgData: uni.getStorageSync('topMsgData_' + uni.getStorageSync('userId')) || [],
		//非置顶数据
		notTopMsgData: uni.getStorageSync('notTopMsgData_' + uni.getStorageSync('userId')) || [],
		//消息免打扰用户
		notDisturbUserData: uni.getStorageSync('notDisturbUserData_' + uni.getStorageSync('userId')) || [],
		//置顶用户
		topUserData: uni.getStorageSync('topUserData_' + uni.getStorageSync('userId')) || [],
		//系统消息数量
		sysNum: uni.getStorageSync('sysNum_' + uni.getStorageSync('userId')) || 0,
		//修改过用户备注的用户
		friendNicknameData: uni.getStorageSync('friendNicknameData_' + uni.getStorageSync('userId')) || [],

	}),
	//相当于异步操作，不能直接改变state的值，可以通过触发mutations来改变
	actions: {
		//收到消息保存到本地
		addChatData(stateData, msgObject) {
			//console.log(msgObject)
			var chatData = stateData.state.chatData
			if (chatData.length == 0) {
				//如果是撤回的消息，切为空，则直接return
				if (msgObject.msgType == 11) {
					return
				}

				var addData = {
					id: msgObject.chatId,
					type: msgObject.chatType,
					list: []
				}
				addData.list.push(msgObject)
				chatData.push(addData)
				stateData.commit('addMsgList', chatData)
				return
			}

			for (var i = 0; i < chatData.length; i++) {
				if (chatData[i].id == msgObject.chatId && chatData[i].type == msgObject.chatType) {
					//如果是撤回的消息
					if (msgObject.msgType == 11) {
						for (var j = 0; j < chatData[i].list.length; j++) {
							if (msgObject.msgId == chatData[i].list[j].msgId) {
								chatData[i].list[j].msgType = 11
								chatData[i].list[j].news = '撤回一条消息'
								stateData.commit('addMsgList', chatData)
								return
							}
						}
						return
					}

					chatData[i].list.unshift(msgObject)
					stateData.commit('addMsgList', chatData)
					return
				}
			}

			var addData = {
				id: msgObject.chatId,
				type: msgObject.chatType,
				list: []
			}
			addData.list.push(msgObject)
			chatData.push(addData)

			stateData.commit('addMsgList', chatData)

		},

		//1，设置msgData的值
		setMsgList(stateData, msgObject) {
			//console.log(msgObject)

			let userId = uni.getStorageSync('userId')
			//如果是自己的错误消息，直接退出
			if (msgObject.id == userId && msgObject.type == 2) {
				return
			}

			//1，获取消息列表
			var topMsgData = stateData.state.topMsgData
			var notTopMsgData = stateData.state.notTopMsgData
			var notDisturbUserData = stateData.state.notDisturbUserData
			var topUserData = stateData.state.topUserData
			var friendNicknameData = stateData.state.friendNicknameData

			//1.2，设置新消息数量
			msgObject.msgNum = 1
			//1.3，设置默认为 不置顶
			var isTop = false
			msgObject.isTop = false

			//2.1，判断是否为勿扰消息
			msgObject.isDisturb = false
			for (var i = 0; i < notDisturbUserData.length; i++) {
				if (notDisturbUserData[i].id == msgObject.id && notDisturbUserData[i].type == msgObject.type) {
					msgObject.msgNum = 0
					msgObject.isDisturb = true
					continue;
				}
			}

			//2.2，判断是否为置顶
			for (var i = 0; i < topUserData.length; i++) {
				if (topUserData[i].id == msgObject.id && topUserData[i].type == msgObject.type) {
					isTop = true
					msgObject.isTop = true
					continue;
				}
			}

			//3，判断是否需要使用昵称
			for (var i = 0; i < friendNicknameData.length; i++) {
				if (friendNicknameData[i].id == msgObject.id && friendNicknameData[i].type == msgObject.type) {
					msgObject.nickname = friendNicknameData[i].updateData
					continue;
				}
			}


			//console.log()

			//3.1，判断置顶消息列表中是否有消息
			if (isTop) {
				if (topMsgData.length == 0) {
					if (msgObject.msgType == 11) {
						return
					}
					topMsgData.push(msgObject)
					stateData.commit('updateTopMsgData', topMsgData)
					return
				} else {
					//3.2，判断是否消息列表中有和消息列表相同的数据
					for (var i = 0; i < topMsgData.length; i++) {
						if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
							//如果是撤回消息且不是最后一条则 不改变
							if (msgObject.msgType == 11) {
								if (topMsgData[i].msgId != msgObject.msgId) {
									return
								}
								//console.log(msgObject.msgId)
							}
							if (msgObject.msgType != 21) {
								//新消息 +1
								msgObject.msgNum = topMsgData[i].msgNum + 1
							} else {
								msgObject.msgNum = notTopMsgData[i].msgNum
							}

							//删除该位置的数据
							topMsgData.splice(i, 1)
							//将消息置顶
							//console.log(msgObject)
							topMsgData.unshift(msgObject)
							stateData.commit('updateTopMsgData', topMsgData)
							return
						}
					}

					topMsgData.unshift(msgObject)
					stateData.commit('updateTopMsgData', topMsgData)
					return
				}
			}

			//4.1
			if (notTopMsgData.length == 0) {
				if (msgObject.msgType == 11) {
					return
				}
				notTopMsgData.push(msgObject)
				stateData.commit('updateNotTopMsgData', notTopMsgData)
				return
			} else {
				//4.2，判断是否消息列表中有和消息列表相同的数据
				for (var i = 0; i < notTopMsgData.length; i++) {
					if (notTopMsgData[i].id == msgObject.id && notTopMsgData[i].type == msgObject.type) {
						//console.log(msgObject)
						//如果是撤回消息且不是最后一条则 不改变
						if (msgObject.msgType == 11) {
							if (notTopMsgData[i].msgId != msgObject.msgId) {
								return
							}
							//console.log(msgObject.msgId)
						}

						if (msgObject.msgType != 21) {
							//新消息 +1
							msgObject.msgNum = notTopMsgData[i].msgNum + 1
						} else {
							msgObject.msgNum = notTopMsgData[i].msgNum
						}

						//删除该位置的数据
						notTopMsgData.splice(i, 1)
						//将消息置顶
						//console.log(msgObject)
						notTopMsgData.unshift(msgObject)
						stateData.commit('updateNotTopMsgData', notTopMsgData)
						return
					}
				}
				notTopMsgData.unshift(msgObject)
				stateData.commit('updateNotTopMsgData', notTopMsgData)
				return
			}

		},
		//2，消息已读
		msgReag(stateData, msgObject) {
			var topMsgData = stateData.state.topMsgData

			var isTop = false
			//判断是否为置顶消息
			for (var i = 0; i < topMsgData.length; i++) {
				if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
					isTop = true
					continue;
				}
			}

			//判断是否为置顶消息
			if (isTop) {
				for (var i = 0; i < topMsgData.length; i++) {
					if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
						topMsgData[i].msgNum = 0
						stateData.commit('updateTopMsgData', topMsgData)
						return
					}
				}
			} else {
				var notTopMsgData = stateData.state.notTopMsgData
				for (var i = 0; i < notTopMsgData.length; i++) {
					if (notTopMsgData[i].id == msgObject.id && notTopMsgData[i].type == msgObject.type) {
						notTopMsgData[i].msgNum = 0
						stateData.commit('updateNotTopMsgData', notTopMsgData)
						return
					}
				}
			}
		},
		//3，消息置顶或非置顶
		msgGoTop(stateData, msgObject) {
			var topMsgData = stateData.state.topMsgData
			var notTopMsgData = stateData.state.notTopMsgData
			var topUserData = stateData.state.topUserData

			//console.log(msgObject)
			//msgObject.isTop = !msgObject.isTop
			//console.log(msgObject.isTop)
			//判断是否为置顶消息
			if (msgObject.isTop) {
				//消息取消置顶
				for (var i = 0; i < topMsgData.length; i++) {
					if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
						//删除该位置的数据
						topMsgData.splice(i, 1)
						//将消息置顶
						msgObject.isTop = false
						notTopMsgData.unshift(msgObject)
						continue
					}
				}
				for (var i = 0; i < topUserData.length; i++) {
					if (topUserData[i].id == msgObject.id && topUserData[i].type == msgObject.type) {
						//删除该位置的数据
						topUserData.splice(i, 1)
						continue
					}
				}
			} else {
				//消息置顶
				for (var i = 0; i < notTopMsgData.length; i++) {
					if (notTopMsgData[i].id == msgObject.id && notTopMsgData[i].type == msgObject.type) {
						//删除该位置的数据
						notTopMsgData.splice(i, 1)
						//将消息置顶
						msgObject.isTop = true
						topMsgData.unshift(msgObject)
						continue
					}
				}
				topUserData.push(msgObject)
			}
			stateData.commit('updateTopMsgData', topMsgData)
			stateData.commit('updateNotTopMsgData', notTopMsgData)
			stateData.commit('updateTopUserData', topUserData)

		},
		//4，删除消息列表中的某项消息
		clearMsgByList(stateData, msgObject) {
			var topMsgData = stateData.state.topMsgData
			console.log(topMsgData)

			var isTop = false
			//判断是否为置顶消息
			for (var i = 0; i < topMsgData.length; i++) {
				if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
					isTop = true
					continue;
				}
			}

			if (isTop) {
				for (var i = 0; i < topMsgData.length; i++) {
					if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
						topMsgData.splice(i, 1)
						stateData.commit('updateTopMsgData', topMsgData)
						return
					}
				}
			} else {
				var notTopMsgData = stateData.state.notTopMsgData
				for (var i = 0; i < notTopMsgData.length; i++) {
					if (notTopMsgData[i].id == msgObject.id && notTopMsgData[i].type == msgObject.type) {
						notTopMsgData.splice(i, 1)
						stateData.commit('updateNotTopMsgData', notTopMsgData)
						return
					}
				}
			}
		},
		//5，清空本地消息
		clearLocalMsgList(stateData, msgObject) {
			//console.log(msgObject)
			var chatData = stateData.state.chatData
			var topMsgData = stateData.state.topMsgData
			var notTopMsgData = stateData.state.notTopMsgData

			for (var i = 0; i < chatData.length; i++) {
				if (chatData[i].id == msgObject.id && chatData[i].type == msgObject.type) {
					chatData.splice(i, 1)
					continue
				}
			}

			for (var i = 0; i < topMsgData.length; i++) {
				if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
					topMsgData.splice(i, 1)
					continue
				}
			}

			for (var i = 0; i < notTopMsgData.length; i++) {
				if (notTopMsgData[i].id == msgObject.id && notTopMsgData[i].type == msgObject.type) {
					notTopMsgData.splice(i, 1)
					continue
				}
			}

			stateData.commit('addMsgList', chatData)
			stateData.commit('updateTopMsgData', topMsgData)
			stateData.commit('updateNotTopMsgData', notTopMsgData)

		},
		//6，消息免打扰
		addDisturbUserList(stateData, msgObject) {
			var notDisturbUserData = stateData.state.notDisturbUserData
			var topMsgData = stateData.state.topMsgData
			var notTopMsgData = stateData.state.notTopMsgData

			//是否消息勿扰
			if (msgObject.isDisturb) {
				//已经是消息勿扰，删除勿扰
				for (var i = 0; i < notDisturbUserData.length; i++) {
					if (notDisturbUserData[i].id == msgObject.id && notDisturbUserData[i].type == msgObject.type) {
						notDisturbUserData.splice(i, 1)
						continue
					}
				}

				if (msgObject.isTop) {
					//修改状态
					for (var i = 0; i < topMsgData.length; i++) {
						if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
							topMsgData[i].isDisturb = false
							continue
						}
					}
				} else {
					//修改状态
					for (var i = 0; i < notTopMsgData.length; i++) {
						if (notTopMsgData[i].id == msgObject.id && notTopMsgData[i].type == msgObject.type) {
							notTopMsgData[i].isDisturb = false
							continue
						}
					}
				}

			} else {
				notDisturbUserData.push(msgObject)

				if (msgObject.isTop) {
					//修改状态
					for (var i = 0; i < topMsgData.length; i++) {
						if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
							topMsgData[i].isDisturb = true
							continue
						}
					}
				} else {
					//修改状态
					for (var i = 0; i < notTopMsgData.length; i++) {
						if (notTopMsgData[i].id == msgObject.id && notTopMsgData[i].type == msgObject.type) {
							notTopMsgData[i].isDisturb = true
							continue
						}
					}
				}
			}

			stateData.commit('updateDisturbUserData', notDisturbUserData)
			stateData.commit('updateTopMsgData', topMsgData)
			stateData.commit('updateNotTopMsgData', notTopMsgData)

		},
		//7，更改信息（1：昵称；2：头像）
		updateInfo(stateData, msgObject) {
			var topMsgData = stateData.state.topMsgData
			var notTopMsgData = stateData.state.notTopMsgData

			for (var i = 0; i < topMsgData.length; i++) {
				if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
					if (msgObject.updateType == 1) {
						topMsgData[i].nickname = msgObject.updateData
						stateData.commit('updateTopMsgData', topMsgData)
						//昵称存入本地
						stateData.commit('updateFriendNickname', msgObject)

						return
					}
					if (msgObject.updateType == 2) {
						topMsgData[i].head = msgObject.updateData
						stateData.commit('updateTopMsgData', topMsgData)
						return
					}
				}
			}

			for (var i = 0; i < notTopMsgData.length; i++) {
				if (notTopMsgData[i].id == msgObject.id && notTopMsgData[i].type == msgObject.type) {
					if (msgObject.updateType == 1) {
						notTopMsgData[i].nickname = msgObject.updateData
						stateData.commit('updateNotTopMsgData', notTopMsgData)

						//昵称存入本地
						stateData.commit('updateFriendNickname', msgObject)
						return
					}
					if (msgObject.updateType == 2) {
						notTopMsgData[i].head = msgObject.updateData
						stateData.commit('updateNotTopMsgData', notTopMsgData)
						return
					}
				}
			}
		},
		//8，新增系统消息
		addsysNum(stateData) {
			var sysNum = stateData.state.sysNum + 1
			stateData.commit('updateSysNum', sysNum)
		},
		//9，系统消息已读
		readSysNum(stateData) {
			stateData.commit('updateSysNum', 0)
		},

		//10，红包已读
		readRedPkgStore(stateData, msgObject) {
			var chatData = stateData.state.chatData

			for (var i = 0; i < chatData.length; i++) {
				if (chatData[i].id == msgObject.id && chatData[i].type == msgObject.type) {
					for (var j = 0; j < chatData[i].list.length; j++) {
						if (chatData[i].list[j].msgId == msgObject.msgId) {
							chatData[i].list[j].isRedRead = true
							stateData.commit('addMsgList', chatData)
							return
						}

					}
				}
			}
		},

		//11，重新上线未读消息添加
		addNotReadMsg(stateData, res) {
			//console.log(12312312313)

			for (var i = 0; i < res.length; i++) {
				var that = this
				//console.log(res[i])
				//好友消息
				if (res[i].chatType == uni.$chatType.CHAT_FRIEND) {
					//console.log(res)
					var id = res[i].senderId
					var head = res[i].senderHead
					var name = res[i].senderName
					if (res[i].senderId == this.userId) {
						//console.log(44444444444)
						id = res[i].receiverId
						name = res[i].receiverName
						head = res[i].receiverHead
					}

					var addMsgHomeData = msgHomeData(
						id,
						2,
						name,
						head,
						res[i].message,
						res[i].time + '',
						1,
						res[i].types,
						res[i].id
					)

					//console.log(addMsgHomeData)
					stateData.dispatch('setMsgList', addMsgHomeData)
					//setMsgList(addMsgHomeData)

					//msgId, tip, news, sendId, sendHead, sendNickname, msgType, time
					var addMsgChatData = msgChatData(
						res[i].id,
						res[i].tip,
						res[i].message,
						res[i].senderId,
						res[i].senderHead,
						res[i].senderName,
						res[i].types,
						res[i].time + '',
						2,
						id,
						res[i].redPkgId
					)
					//console.log(addMsgChatData)
					stateData.dispatch('addChatData', addMsgChatData)
				} else if (res[i].chatType == uni.$chatType.CHAT_GROUP || res[i].chatType == uni.$chatType
					.QUIT_GROUP || res[i].chatType == uni.$chatType.KICK_OUT_GROUP || res[i].chatType == uni.$chatType
					.ESTOPPEL || res[i].chatType == uni.$chatType.DISSOLUTION_GROUP) { //群聊消息
					//console.log('console.log(res)')
					//console.log(res)
					var addMsgHomeData = msgHomeData(
						res[i].groupId,
						3,
						res[i].groupName,
						res[i].groupHead,
						res[i].message,
						res[i].time + '',
						1,
						res[i].types,
						res[i].id
					)
					//console.log(addMsgData)
					stateData.dispatch('setMsgList', addMsgHomeData)

					//console.log(res.params)
					//msgId, tip, news, sendId, sendHead, sendNickname, msgType, time
					var addMsgChatData = msgChatData(
						res[i].id,
						res[i].tip,
						res[i].message,
						res[i].senderId,
						res[i].senderHead,
						res[i].senderName,
						res[i].types,
						res[i].time + '',
						3,
						res[i].groupId,
						res[i].redPkgId
					)
					stateData.dispatch('addChatData', addMsgChatData)
				} else {
					//系统消息 (chatType == 4添加好友，chatType == 8，拉取好友进入群聊)
					stateData.dispatch('addsysNum')
					//that.sysNumData = that.sysNum
				}
			}
		},
		//12，消息删除
		removeMsvById(stateData, msgObject) {
			//1，获取消息列表
			var topMsgData = stateData.state.topMsgData
			var notTopMsgData = stateData.state.notTopMsgData
			var topUserData = stateData.state.topUserData
			var chatData = stateData.state.chatData

			//2，修改聊天信息
			var updateMsgInfo = {}
			var isChange = false
			for (var i = 0; i < chatData.length; i++) {
				if (chatData[i].id == msgObject.chatId && chatData[i].type == msgObject.chatType) {
					for (var j = 0; j < chatData[i].list.length; j++) {
						if (chatData[i].list[j].msgId == msgObject.msgId) {
							if (j == 0) {
								if (chatData[i].list.length == 1) {
									isChange = true
								} else {
									updateMsgInfo = chatData[i].list[j + 1]
								}
							}
							chatData[i].list.splice(j, 1)
							stateData.commit('addMsgList', chatData)
							continue
						}
					}
				}
			}

			//2，判断是否为置顶
			var isTop = false
			for (var i = 0; i < topUserData.length; i++) {
				if (topUserData[i].id == msgObject.id && topUserData[i].type == msgObject.type) {
					isTop = true
					continue;
				}
			}
			//3，更改指定或非置顶消息
			if (isTop) {
				for (var i = 0; i < topMsgData.length; i++) {
					if (topMsgData[i].id == msgObject.chatId && topMsgData[i].type == msgObject.chatType) {
						if ((JSON.stringify(updateMsgInfo) == "{}")) {
							if (isChange) {
								topMsgData.splice(i, 1)
								stateData.commit('updateTopMsgData', topMsgData)
							}
							// topMsgData.splice(i, 1)
							// stateData.commit('updateTopMsgData', topMsgData)
						} else {
							topMsgData[i].news = updateMsgInfo.news
							topMsgData[i].nickname = updateMsgInfo.sendNickname
							topMsgData[i].time = updateMsgInfo.time
							stateData.commit('updateTopMsgData', topMsgData)
						}
					}
				}
			} else {
				for (var i = 0; i < notTopMsgData.length; i++) {
					if (notTopMsgData[i].id == msgObject.chatId && notTopMsgData[i].type == msgObject.chatType) {
						if ((JSON.stringify(updateMsgInfo) == "{}")) {
							if (isChange) {
								notTopMsgData.splice(i, 1)
								stateData.commit('updateNotTopMsgData', notTopMsgData)
							}
							// notTopMsgData.splice(i, 1)
							// stateData.commit('updateNotTopMsgData', notTopMsgData)
						} else {
							notTopMsgData[i].news = updateMsgInfo.news
							notTopMsgData[i].nickname = updateMsgInfo.sendNickname
							notTopMsgData[i].time = updateMsgInfo.time
							notTopMsgData[i].msgType = updateMsgInfo.msgType
							stateData.commit('updateNotTopMsgData', notTopMsgData)
						}
					}
				}
			}
		},


		//13，删除某个备注
		removeFriendRemarks(stateData, msgObject) {
			var topMsgData = stateData.state.topMsgData
			var notTopMsgData = stateData.state.notTopMsgData
			console.log(msgObject)
			for (var i = 0; i < topMsgData.length; i++) {
				if (topMsgData[i].id == msgObject.id && topMsgData[i].type == msgObject.type) {
					if (msgObject.updateType == 1) {
						topMsgData[i].nickname = msgObject.updateData
						stateData.commit('updateTopMsgData', topMsgData)
						//昵称存入本地
						stateData.commit('removeMpFriendRemarks', msgObject)

						return
					}
					/* if (msgObject.updateType == 2) {
						topMsgData[i].head = msgObject.updateData
						stateData.commit('updateTopMsgData', topMsgData)
						return
					} */
				}
			}

			for (var i = 0; i < notTopMsgData.length; i++) {
				if (notTopMsgData[i].id == msgObject.id && notTopMsgData[i].type == msgObject.type) {
					if (msgObject.updateType == 1) {
						notTopMsgData[i].nickname = msgObject.updateData
						stateData.commit('updateNotTopMsgData', notTopMsgData)

						//昵称存入本地
						stateData.commit('removeMpFriendRemarks', msgObject)
						return
					}
					/* if (msgObject.updateType == 2) {
						notTopMsgData[i].head = msgObject.updateData
						stateData.commit('updateNotTopMsgData', notTopMsgData)
						return
					} */
				}
			}
		}


	},

	//方法节点（相当于同步操作）
	mutations: {
		//新增聊天消息到本地
		addMsgList(state, msg) {
			state.chatData = msg
			//保存到本地
			uni.setStorageSync('chatData_' + uni.getStorageSync('userId'), msg)
		},

		//新增聊天信息
		updateTopMsgData(state, msg) {
			state.topMsgData = msg
			//保存到本地
			uni.setStorageSync('topMsgData_' + uni.getStorageSync('userId'), msg)
		},
		updateNotTopMsgData(state, msg) {
			state.notTopMsgData = msg
			//保存到本地
			uni.setStorageSync('notTopMsgData_' + uni.getStorageSync('userId'), msg)
		},

		//新增置顶用户
		updateTopUserData(state, msg) {
			state.topUserData = msg
			//保存到本地
			uni.setStorageSync('topUserData_' + uni.getStorageSync('userId'), msg)
		},

		//修改勿扰数据
		updateDisturbUserData(state, msg) {
			state.notDisturbUserData = msg
			//保存到本地
			uni.setStorageSync('notDisturbUserData_' + uni.getStorageSync('userId'), msg)
		},

		//修改系统消息数量
		updateSysNum(state, msg) {
			state.sysNum = msg
			//保存到本地
			uni.setStorageSync('sysNum_' + uni.getStorageSync('userId'), msg)
		},

		//昵称存入本地
		updateFriendNickname(state, msg) {
			var nameData = state.friendNicknameData
			for (var i = 0; i < nameData.length; i++) {
				if (nameData[i].id == msg.id) {
					nameData[i] = msg
					state.friendNicknameData = nameData

					uni.setStorageSync('friendNicknameData_' + uni.getStorageSync('userId'), nameData)
					return
				}
			}
			nameData.push(msg)
			state.friendNicknameData = nameData

			uni.setStorageSync('friendNicknameData_' + uni.getStorageSync('userId'), nameData)
			return
		},

		removeMpFriendRemarks(state, msg) {
			var nameData = state.friendNicknameData
			for (var i = 0; i < nameData.length; i++) {
				if (nameData[i].id == msg.id) {
					nameData.splice(i, 1)
					state.friendNicknameData = nameData

					uni.setStorageSync('friendNicknameData_' + uni.getStorageSync('userId'), nameData)
					return
				}
			}
		},

		emptyAll(state) {
			console.log('执行了')
			// console.log(state)

			state.chatData = []
			state.topMsgData = []
			state.notTopMsgData = []
			state.sysNum = 0

			//消息免打扰用户
			state.notDisturbUserData = []
			//置顶用户
			state.topUserData = []
			//修改过用户备注的用户
			state.friendNicknameData = []

			//清空token
			uni.setStorageSync('token', '')
			//清空用户登录模式
			uni.setStorageSync('isVisitorLogin', false);
			//清空用户实名认证
			uni.setStorageSync('identifyCheck', false)

			//uni.setStorageSync('userId', 0)

			//uni.clearStorageSync()

			//console.log('执行完毕')
			//保存到本地
			// uni.setStorageSync('chatData_' + uni.getStorageSync('userId'), [])
			// uni.setStorageSync('topMsgData_' + uni.getStorageSync('userId'), [])
			// uni.setStorageSync('notTopMsgData_' + uni.getStorageSync('userId'), [])
			// uni.setStorageSync('sysNum_' + uni.getStorageSync('userId'), 0)
		},
		cleanChatMsg(state) {
			state.chatData = []
			state.topMsgData = []
			state.notTopMsgData = []
			state.sysNum = 0
			var userId = uni.getStorageSync('userId')

			uni.setStorageSync('chatData_' + userId, [])
			uni.setStorageSync('topMsgData_' + userId, [])
			uni.setStorageSync('notTopMsgData_' + userId, [])
			uni.setStorageSync('sysNum_' + userId, 0)

		},


		//初始化 state的值
		initStateValue(state, userId) {
			//console.log("新的用户ID：" + userId)
			//console.log('消息初始化！')
			//聊天数据
			state.chatData = uni.getStorageSync('chatData_' + userId) || []
			//置顶数据
			state.topMsgData = uni.getStorageSync('topMsgData_' + userId) || []
			//非置顶数据
			state.notTopMsgData = uni.getStorageSync('notTopMsgData_' + userId) || []
			//消息免打扰用户
			state.notDisturbUserData = uni.getStorageSync('notDisturbUserData_' + userId) || []
			//置顶用户
			state.topUserData = uni.getStorageSync('topUserData_' + userId) || []
			//系统消息数量
			state.sysNum = uni.getStorageSync('sysNum_' + userId) || 0
			//修改过用户备注的用户
			state.friendNicknameData = uni.getStorageSync('sysNum_' + userId) || []
		}
	},

	getters: {
		// state的计算属性
		//用 this.$store.getters.getData()读取
		getData: (state) => {
			return state;
		},

		getChatData: (state) => {
			return state.chatData;
		},

		//是否置顶
		getIsTop: (state) => (msgObject) => {
			var topData = state.topMsgData
			for (var i = 0; i < topData.length; i++) {
				if (topData[i].id == msgObject.id && topData[i].type == msgObject.type) {
					return true;
				}
			}

			return false
		},
		//是否勿扰
		getIsDisturb: (state) => (msgObject) => {
			console.log(msgObject)
			var notDisturbUserData = state.notDisturbUserData
			for (var i = 0; i < notDisturbUserData.length; i++) {
				if (notDisturbUserData[i].id == msgObject.id && notDisturbUserData[i].type == msgObject.type) {
					return true;
				}
			}
			return false
		}
	}
}